//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================
// Code file original author: Martin Lapierre, http://devinstinct.com
//===============================================================================

using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.Design.ValidatorNodes;
using EntLibContrib.Validation.Configuration.Design.Properties;

namespace EntLibContrib.Validation.Configuration.Design.Nodes
{
	/// <summary>
	/// Respresents the designtime configuration node for an <see cref="TypeValidatorData"/>.
	/// </summary>
	/// <remarks>
	/// Pretty much the same thing than <see cref="TypeConversionValidatorNode"/>.
	/// </remarks>
	public class TypeValidatorNode : ValueValidatorNode
	{
		#region Fields
		private string targetTypeName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the fully qualified assembly name of the target element type.
		/// </summary>
		/// <value>The fully qualified assembly name of the target element type.</value>
		[Required]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(object))]
		[SRDescription("TargetTypeDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public string TargetTypeName
		{
			get { return targetTypeName; }
			set { targetTypeName = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Creates an instance of <see cref="TypeValidatorNode"/> based on default values.
		/// </summary>
		public TypeValidatorNode()
			: this(new TypeValidatorData(Resources.TypeValidatorNodeName))
		{
		}

		/// <summary>
		/// Creates an instance of <see cref="TypeValidatorNode"/> based on runtime configuration data.
		/// </summary>
		/// <param name="validatorData">The corresponding runtime configuration data.</param>
		public TypeValidatorNode(TypeValidatorData validatorData)
			: base(validatorData)
		{
			this.targetTypeName = validatorData.TargetTypeName;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Returns the runtime configuration data that is represented by this node.
		/// </summary>
		/// <returns>
		/// An instance of <see cref="TypeValidatorData"/> that can be persisted to a configuration file.
		/// </returns>
		public override ValidatorData CreateValidatorData()
		{
			TypeValidatorData validatorData = new TypeValidatorData(Name);
			SetValidatorBaseProperties(validatorData);
			SetValueValidatorBaseProperties(validatorData);

			validatorData.TargetTypeName = targetTypeName;

			return validatorData;
		}
		#endregion
	}
}
